﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using HKQuery.Common;

namespace HKQuery.Areas.Front.Controllers
{
    public class QueryController : Controller
    {
        //
        // GET: /Front/Query/
        BLL.Query QueryBLL = new BLL.Query();
        int PageSize = int.Parse(System.Configuration.ConfigurationManager.AppSettings["PageSize"].ToString());
        public ActionResult Index(int id = 1)
        {
            string user = OperateSession.get("user");

            if (string.IsNullOrEmpty(user))
            {
                Response.Redirect(Url.Content("~/Home/Index"));
                Response.Flush();
                Response.End();

                return null;
            }
            string selszsc = Fetch.Post("selszsc");

            string txtszsc = "";
            if (!string.IsNullOrEmpty(selszsc) & selszsc != "3")
            {
                txtszsc = " and  szsc='" + selszsc + "'";
            }
            string rqStart = Fetch.Post("rqStart");
            if (string.IsNullOrEmpty(rqStart))
            {
                rqStart = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToString("yyyy-MM-dd");
            }
            ViewData["rqStart"] = rqStart;
            string rqEnd = Fetch.Post("rqEnd");
            if (string.IsNullOrEmpty(rqEnd))
            {
                rqEnd = DateTime.Now.ToString("yyyy-MM-dd");
            }
            ViewData["rqEnd"] = rqEnd;
            string cxlx = Fetch.Post("cxlx");

            if (string.IsNullOrEmpty(cxlx))
            {
                ViewData["selcxlx"] = "1";
                cxlx = "1";
            }
            else
            {
                ViewData["selcxlx"] = cxlx;
            }
            string gp1 = " b.twh,tz ";
            string gp2 = " CONVERT(varchar(6),jkrq,112) ";
            string gp3 = "  CONVERT(varchar(8),jkrq,112) ";
            string gp = cxlx == "3" ? gp3 : cxlx == "2" ? gp2 : gp1;

            string xm1 = " b.twh+'('+Rtrim(tz)+')'  ";
            string xm2 = " CONVERT(varchar(6),jkrq,112) ";
            string xm3 = " CONVERT(varchar(8),jkrq,112) ";
            string xm = cxlx == "3" ? xm3 : cxlx == "2" ? xm2 : xm1;
            string od1 = " b.twh ";
            string od2 = " CONVERT(varchar(6),jkrq,112)  ";
            string od3 = "  CONVERT(varchar(8),jkrq,112) ";
            string od = cxlx == "3" ? od3 : cxlx == "2" ? od2 : od1;

            string fm1 = @"
 From twxx b left join tbljkxx a
   On a.twh=b.twh";
            string fm2 = @"From tbljkxx a,twxx b ";


            string fm = cxlx == "1" ? fm1 : fm2;
            
            string wherestart1 = " (Zt_dm='1' or zt_dm is null) ";
            string wherestart2 = "  a.twh=b.twh ";
            string wherestart = cxlx == "1" ? wherestart1 : wherestart2;
            DataTable dt = QueryBLL.GetList(cxlx, string.IsNullOrEmpty(rqStart) & string.IsNullOrEmpty(rqEnd) ? "" : " and jkrq BETWEEN '" + (string.IsNullOrEmpty(rqStart) ? DateTime.MinValue.ToString("yyyy-MM-dd") : rqStart) + "' AND '" + (string.IsNullOrEmpty(rqEnd) ? DateTime.MaxValue.ToString("yyyy-MM-dd") : rqEnd) + "' ", wherestart + txtszsc, gp, xm, od, fm, id, PageSize);
            DataTable dtHJ = QueryBLL.GetListHJ(cxlx, string.IsNullOrEmpty(rqStart) & string.IsNullOrEmpty(rqEnd) ? "" : " and jkrq BETWEEN '" + (string.IsNullOrEmpty(rqStart) ? DateTime.MinValue.ToString("yyyy-MM-dd") : rqStart) + "' AND '" + (string.IsNullOrEmpty(rqEnd) ? DateTime.MaxValue.ToString("yyyy-MM-dd") : rqEnd) + "' ", wherestart + txtszsc, gp, xm, od, fm, id, PageSize);
            ViewData["dtHJ"] = dtHJ;
            int total = dt.Rows.Count > 0 ? int.Parse(dt.Rows[0]["ct"].ToString()) : 0;
            ViewData["total"] = total;
            int pageCount = (int)total / PageSize;
            if (total % PageSize != 0)
            {
                pageCount = pageCount + 1;
            }
            ViewData["allPage"] = pageCount;
            ViewData["page"] = id;
            ViewData["srcurl"] = Url.Action("Index", "Query") + "/?id=";
            List<SelectListItem> szsc = new List<SelectListItem>() { new SelectListItem() { Text = "全部", Value = "3", Selected = true }, new SelectListItem() { Text = "日湖店", Value = "1" }, new SelectListItem() { Text = "联丰店", Value = "2" } };
            szsc.ForEach(h => h.Selected = h.Value.Trim() == selszsc.Trim());
            ViewData["szsc"] = szsc;

            return View(dt);
        }

    }
}
